﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SkiaSharp;
using Solution.Comm.Code;
using Solution.Services.Employee;
using Web.Areas.Admin.Models;
using static Web.Filters.CheckPermissionFilter;

namespace Web.Areas.Admin
{

    
    [Area("Admin")]
    public class LoginController : Controller
    {
        private readonly IEmployeeServices _employeeServices;
        private readonly IHttpContextAccessor _httpContextAccessor;
      
        public LoginController(IEmployeeServices employeeServices,IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
            _employeeServices = employeeServices;
        }
        [NoPermissionRequired]
        [HttpGet]
        public IActionResult Index()
        {
            return View();
        }
        [NoPermissionRequired]
        [HttpPost]
        public IActionResult LoinCheck(LoginModel loginModel)
        {

            if (ModelState.IsValid) {
                //验证码后期
               var c = _employeeServices.Login(loginModel.LoginName, loginModel.PassWord);
              string Tempcode = TempData["SecurityCode"] as string;
              if (Tempcode.ToLower() != loginModel.Code ) {

                  return Content("验证码错误");
              }
                string userinfo = JsonConvert.SerializeObject(c);
                _httpContextAccessor.HttpContext.Session.SetString("user", userinfo);

                string key = _httpContextAccessor.HttpContext.Session.GetString("user");

                return Redirect("Home");
            }
            else {

            }

            return Redirect("Index");
        }

        [NoPermissionRequired]
        [HttpGet]
        public IActionResult Code()
        {

            SkiaSharpHelper skiaSharpHelper = new SkiaSharpHelper();
           
            string code = skiaSharpHelper.RndNum(4);
            TempData["SecurityCode"] = code;
            using (SKImage img = SKImage.FromBitmap(skiaSharpHelper.Code(code))) {

                using (SKData p = img.Encode()) {

                    return File(p.ToArray(), "image/Png");

                }
            }
          
             
           
        }

        public IActionResult Home()
        {

          

                return View();
        }



    }
}